<?php
class Admin_JsonController extends Zend_Controller_Action
{
    
    public function init()
    {
        $this->getHelper('layout')->setLayout('json');
    }
    
    public function getParentLocationOptionsAction()
    {
        require_once 'Locations.php';
        echo $this->_encode(Locations::getParentLocationOptions(
                (int) $this->_request->getParam('id'), $this->_request->getParam('cat')));
        exit;
    } 
        
    public function getBusinessTypeOptionsAction()
    {
        require_once 'BusinessTypes.php';
        echo $this->_encode(BusinessTypes::getTypeOptions($this->_request->getParam('activity')));
        exit;
    } 
    
    public function getRoleOptionsAction()
    {
        require_once 'Roles.php';
        echo $this->_encode(Roles::getRoleOptions());
        exit;
    } 
                
    public function getGroupClientOptionsAction()
    {
        require_once 'Clients.php';
        echo $this->_encode(Clients::getClientOptions('Group', (int) $this->_request->getParam('id')));
        exit;
    } 
    
    public function getOfficerGroupOptionsAction()
    {
        require_once 'Groups.php';
        echo $this->_encode(Groups::getGroupOptions('Officer', (int) $this->_request->getParam('id')));
        exit;
    }
            
    public function getOfficerCommunityOptionsAction()
    {
        require_once 'Communities.php';
        echo $this->_encode(Communities::getCommunityOptions('Officer', (int) $this->_request->getParam('id')));
        exit;
    }
    
    public function getGroupCommunityOptionsAction()
    {
        require_once 'Communities.php';
        echo $this->_encode(Communities::getCommunityOptions('Group', (int) $this->_request->getParam('id')));
        exit;
    }
    
    public function getLineProgramOptionsAction()
    {
        require_once 'Programs.php';
        echo $this->_encode(Programs::getProgramOptions('Line', (int) $this->_request->getParam('id')));
        exit;
    }
    
    public function getProgramProductOptionsAction()
    {
        require_once '../application/modules/loan/models/LoanProducts.php';
        echo $this->_encode(LoanProducts::getLoanProductOptions('Program', (int) $this->_request->getParam('id')));
        exit;
    }
    
    public function getLoanProductDataAction()
    {
        require_once '../application/modules/loan/models/LoanProducts.php';
        echo $this->_encode(LoanProducts::getLoanProductData((int) $this->_request->getParam('id')));
        exit;
    }
    
    public function addGroupAction()
    {        
        require_once 'Groups.php';
        $bankId = (int) $this->_request->getParam('id');
        Groups::add($bankId);
        echo $this->_encode(Groups::getGroupOptions('Bank', $bankId));
        exit;
    }
    
    public function deactivateGroupAction()
    {
        require_once 'Groups.php';
        $groupId = (int) $this->_request->getParam('id');
        $bankId = Groups::deactivate($groupId);
        echo $this->_encode(Groups::getGroupOptions('Bank', $bankId));
        exit;
    }
    
    protected function _encode($data)
    {
        $options = array();
        $json = '[';
        foreach ($data as $oV => $oT) { $oT = addslashes($oT); $options[] = "{oV: '$oV', oT: '$oT'}"; }
        $json .= implode(',', $options);
        $json .= ']';
        return $json;        
    }
}